<template>
  <div class="bigBox">
    <!-- 搜索 -->
    <div class="search">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="设备编号">
          <el-input v-model="value" placeholder="请输入" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 内容 -->
    <el-table
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column
        type="index"
        prop="date"
        label="序号"
        width="140"
      />
      <el-table-column
        prop="innerCode"
        label="设备编号"
        width="280"
      />
      <el-table-column
        prop="type.name"
        label="设备型号"
        width="300"
      />
      <el-table-column
        prop="node.name"
        label="详细地址"
        width="280"
      />
      <el-table-column
        prop="vmStatus"
        label="运营状态"
        width="280"
      />
      <el-table-column
        prop="address"
        label="设备状态"
        width="300"
      >
        <template>
          <span class="orange">离线</span>
          <span class="green">货到</span>
          <span class="green">传送轴</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="address"
        label="操作"
      >
        <template #default="{row}">
          <el-link type="primary" :underline="false" @click="viewDetails(row)">查看详情</el-link>
        </template>
      </el-table-column>
    </el-table>
    <!-- 下面 -->
    <el-row class="bottom">
      <el-col :span="20">
        <div>共{{ areaList.totalCount }}条数据 第{{ areaList.pageIndex }}/{{ areaList.totalPage }}页</div>
      </el-col>
      <el-col :span="4">
        <el-button-group>
          <el-button type="warning" icon="el-icon-arrow-left" :disabled="upShow" @click="up">上一页</el-button>
          <el-button type="warning" style="margin-left:20px;" :disabled="underShow" @click="under">下一页<i class="el-icon-arrow-right el-icon--right" /></el-button>
        </el-button-group>
      </el-col>
    </el-row>
    <!-- 查看详情的弹出层 -->
    <el-dialog
      style="padding:20px"
      title="设备详情"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-row style="background-color: #f4f6fb;padding:20px">
        <el-col :span="6">销售量: <span>{{ details.Sales }}个</span> </el-col>
        <el-col :span="6">销售额: <span>{{ details.sell }}万元</span></el-col>
        <el-col :span="6">补货次数: <span>{{ details.replenishment }}次</span></el-col>
        <el-col :span="6">维修次数: <span>{{ details.repair }}次</span></el-col>
      </el-row>
      <div style="margin-top:20px">商品销量(月)</div>
      <el-row class="bg">
        <el-col v-for="(item,index) in salesList" :key="index" :span="6">{{ item.skuName }}:{{ item.count }}  </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import moment from 'moment'
import { salesQuantity, repairApi, replenishment, rangeOrder, searchMachine, income } from '@/api'
export default {
  data() {
    return {
      dialogVisible: false, // 查看详情
      upShow: false, // 上下页的禁用状态
      underShow: false,
      value: '', // 搜索框绑定的数据
      tableData: [], // 渲染列表
      areaList: [], // 全部的数据
      details: {
        // 详情的数据
        Sales: '',
        sell: '',
        replenishment: '',
        repair: ''
      },
      salesList: []// 详情里的渲染
    }
  },
  created() {
    this.publicFn()
  },
  methods: {
    handleClose() {
      // 查看详情的关闭方法
      this.dialogVisible = false
    },
    async viewDetails(row) {
      console.log(row)
      // 查看详情
      const time = new Date().getTime()
      const end = new Date().getMonth() + 1
      const num = moment(time).format('YYYY-MM-DD' + ' HH:mm:ss')
      const nums = moment(time).format('YYYY-MM-DD')
      const { data } = await rangeOrder({ end: num, start: `2022-${end}-01 00:00:00`, innerCode: row.innerCode })
      this.details.Sales = data // 销售量
      const res = await income({ end: num, start: `2022-${end}-01 00:00:00`, innerCode: row.innerCode })
      this.details.sell = (res.data / 1000000).toFixed(2)
      const req = await replenishment(row.innerCode, `2022-${end}-01`, nums)
      console.log(req)
      this.details.replenishment = req.data
      const val = await repairApi(row.innerCode, `2022-${end}-01`, nums)
      this.details.repair = val.data
      console.log(this.details)
      const list = await salesQuantity(row.innerCode, `2022-${end}-01`, nums)
      this.salesList = list.data
      this.dialogVisible = true
    },
    onSubmit() {
      // 查询
      this.publicFn('', '', '', this.value)
    },
    // 公共调用的方法
    async publicFn(pageIndex, pageSize, status, innerCode) {
      const { data } = await searchMachine({ pageIndex: pageIndex, pageSize: pageSize, status: status, innerCode: innerCode })
      this.areaList = data
      this.tableData = data.currentPageRecords
      if (data.pageIndex <= 1) {
        this.upShow = true
      } else {
        this.upShow = false
      }
      if (data.pageIndex === data.totalPage) {
        this.underShow = true
      } else {
        this.underShow = false
      }
    },
    up() {
      // 上一页
      this.publicFn(+this.areaList.pageIndex - 1, this.areaList.pageSize)
    },
    under() {
      // 下一页
      this.publicFn(+this.areaList.pageIndex + 1, this.areaList.pageSize)
    }
  }

}
</script>
<style lang="scss" scoped>
.orange{
  color: #fff;
  background-color: #ffb733;
  padding:1px 10px;
  display: inline-block;
  border-radius: 15px;
}
.green{
  margin-left: 8px;
  display: inline-block;
  color: #fff;
  background-color: #41cc97;
    padding:1px 10px;
  border-radius: 15px;

}
.bigBox {
  padding: 20px;
  .search {
    background-color: #fff;
    padding: 10px;
    padding-bottom: 0px;
  }
}
.bg{
  .el-col{
    line-height: 44px;
    text-align: center;
    border: 1px solid rgb(188, 187, 187);
    width: 130px;
    height: 44px;
  }
}

</style>
